{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pprint import pprint\n",
    "import tdameritrade as td\n",
    "c = td.TDClient()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# shows private account info\n",
    "# pprint(c.accounts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'AAPL': {'assetType': 'EQUITY',\n",
      "          'cusip': '037833100',\n",
      "          'description': 'Apple Inc. - Common Stock',\n",
      "          'exchange': 'NASDAQ',\n",
      "          'symbol': 'AAPL'}}\n"
     ]
    }
   ],
   "source": [
    "pprint(c.search('aapl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'AAPL': {'52WkHigh': 229.67,\n",
      "          '52WkLow': 150.24,\n",
      "          'askId': 'P',\n",
      "          'askPrice': 225.93,\n",
      "          'askSize': 700,\n",
      "          'assetType': 'EQUITY',\n",
      "          'bidId': 'Q',\n",
      "          'bidPrice': 225.82,\n",
      "          'bidSize': 200,\n",
      "          'bidTick': ' ',\n",
      "          'closePrice': 225.74,\n",
      "          'delayed': True,\n",
      "          'description': 'Apple Inc. - Common Stock',\n",
      "          'digits': 4,\n",
      "          'divAmount': 2.92,\n",
      "          'divDate': '2018-08-10 00:00:00.0',\n",
      "          'divYield': 1.29,\n",
      "          'exchange': 'q',\n",
      "          'exchangeName': 'NASDAQ',\n",
      "          'highPrice': 225.84,\n",
      "          'lastId': 'D',\n",
      "          'lastPrice': 225.93,\n",
      "          'lastSize': 100,\n",
      "          'lowPrice': 224.02,\n",
      "          'marginable': True,\n",
      "          'mark': 225.74,\n",
      "          'nAV': 0.0,\n",
      "          'netChange': 0.19,\n",
      "          'openPrice': 224.79,\n",
      "          'peRatio': 20.16,\n",
      "          'quoteTimeInLong': 1538179186513,\n",
      "          'regularMarketLastPrice': 225.74,\n",
      "          'regularMarketLastSize': 130,\n",
      "          'regularMarketNetChange': 0.0,\n",
      "          'regularMarketTradeTimeInLong': 1538164803974,\n",
      "          'securityStatus': 'Normal',\n",
      "          'shortable': True,\n",
      "          'symbol': 'AAPL',\n",
      "          'totalVolume': 22929364,\n",
      "          'tradeTimeInLong': 1538179186515,\n",
      "          'volatility': 0.0061566425}}\n"
     ]
    }
   ],
   "source": [
    "pprint(c.quote('aapl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'assetType': 'EQUITY',\n",
      "  'cusip': '037833100',\n",
      "  'description': 'Apple Inc. - Common Stock',\n",
      "  'exchange': 'NASDAQ',\n",
      "  'symbol': 'AAPL'}]\n"
     ]
    }
   ],
   "source": [
    "pprint(c.instrument('037833100'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>close</th>\n",
       "      <th>datetime</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>open</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>222.75</td>\n",
       "      <td>1537182000000</td>\n",
       "      <td>222.88</td>\n",
       "      <td>222.55</td>\n",
       "      <td>222.78</td>\n",
       "      <td>2920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>222.59</td>\n",
       "      <td>1537182060000</td>\n",
       "      <td>222.59</td>\n",
       "      <td>222.59</td>\n",
       "      <td>222.59</td>\n",
       "      <td>500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>222.60</td>\n",
       "      <td>1537182120000</td>\n",
       "      <td>222.61</td>\n",
       "      <td>222.60</td>\n",
       "      <td>222.60</td>\n",
       "      <td>1180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>222.50</td>\n",
       "      <td>1537182180000</td>\n",
       "      <td>222.56</td>\n",
       "      <td>222.50</td>\n",
       "      <td>222.56</td>\n",
       "      <td>2850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>222.75</td>\n",
       "      <td>1537182240000</td>\n",
       "      <td>222.75</td>\n",
       "      <td>222.53</td>\n",
       "      <td>222.53</td>\n",
       "      <td>1703</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    close       datetime    high     low    open  volume\n",
       "0  222.75  1537182000000  222.88  222.55  222.78    2920\n",
       "1  222.59  1537182060000  222.59  222.59  222.59     500\n",
       "2  222.60  1537182120000  222.61  222.60  222.60    1180\n",
       "3  222.50  1537182180000  222.56  222.50  222.56    2850\n",
       "4  222.75  1537182240000  222.75  222.53  222.53    1703"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.DataFrame(c.history('AAPL')['candles']).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "os = c.options('AAPL')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'ask': 0.01,\n",
      "  'askSize': 625,\n",
      "  'bid': 0.0,\n",
      "  'bidSize': 0,\n",
      "  'closePrice': 0.0,\n",
      "  'daysToExpiration': 5,\n",
      "  'deliverableNote': '',\n",
      "  'delta': 0.0,\n",
      "  'description': 'AAPL Oct 5 2018 170 Put (Weekly)',\n",
      "  'exchangeName': 'OPR',\n",
      "  'expirationDate': 1538744400000,\n",
      "  'expirationType': 'S',\n",
      "  'gamma': 0.0,\n",
      "  'highPrice': 0.0,\n",
      "  'inTheMoney': False,\n",
      "  'isIndexOption': None,\n",
      "  'last': 0.01,\n",
      "  'lastSize': 0,\n",
      "  'lastTradingDay': 1538712000000,\n",
      "  'lowPrice': 0.0,\n",
      "  'mark': 0.01,\n",
      "  'markChange': 0.0,\n",
      "  'markPercentChange': 4900.0,\n",
      "  'mini': False,\n",
      "  'multiplier': 100.0,\n",
      "  'netChange': 0.01,\n",
      "  'nonStandard': False,\n",
      "  'openInterest': 590,\n",
      "  'openPrice': 0.0,\n",
      "  'optionDeliverablesList': None,\n",
      "  'percentChange': 9900.0,\n",
      "  'putCall': 'PUT',\n",
      "  'quoteTimeInLong': 1538164799058,\n",
      "  'rho': 0.0,\n",
      "  'settlementType': ' ',\n",
      "  'strikePrice': 170.0,\n",
      "  'symbol': 'AAPL_100518P170',\n",
      "  'theoreticalOptionValue': 0.0,\n",
      "  'theoreticalVolatility': 29.0,\n",
      "  'theta': -0.0,\n",
      "  'timeValue': 0.01,\n",
      "  'totalVolume': 0,\n",
      "  'tradeDate': None,\n",
      "  'tradeTimeInLong': 1538076048243,\n",
      "  'vega': 0.0,\n",
      "  'volatility': 0.0}]\n"
     ]
    }
   ],
   "source": [
    "pprint(os['putExpDateMap']['2018-10-05:5']['170.0'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
